/*
	
	@ by Wang Si Yuan

	@ for ekola 

	@ Home Module Service

 */

var homeApp = angular.module("homeApp",["ngRoute","appDirective","appWxShareDirective"]);

homeApp.service("commonService",function($http){
	// post 请求
	this.httpReq = function(url,formDataStr,callback){
		$http({
			url: url,
			method: "post",
		   	data: formDataStr,
		   	headers:{
		   		"Content-Type":"application/x-www-form-urlencoded",
		   	},
		}).success(function(response){
			if( "function" == typeof callback ){
				callback(response);
			}
		}).error(function(){
			console.log("http error...")
		});
	}

	this.checkTages = function(tagName){
		if("undefined" == typeof tagName) return false;

		var reg = /^[\u4E00-\u9FA5\uf900-\ufa2d\w\!\！\.\~\ ]{2,10}$/;
		if(!reg.test(tagName)){
			return false;
		}else{
			return true;
		}
	}

	this.totalPageList = function(curPage,totalPage){
		var list = [];

		if(totalPage <= 10){
			var pageStart = 1;
			var pageEnd = totalPage;
		}else if(totalPage - curPage > 5 && curPage >= 10){
			var pageStart = curPage - 4;
			var pageEnd = curPage + 5;
		}else if(totalPage - curPage <= 5 && curPage >= 10){
			var pageStart = totalPage - 10;
			var pageEnd = totalPage;
		}else if( curPage < 10 && totalPage > 10 ){
			var pageStart = 1;
			var pageEnd = 10;
		}
		for(var i = pageStart ; i <= pageEnd; i++){
			list.push(i);
		}

		return list;
	}

	// emoji 表情处理
	this.utf16toEntities = function(str) {
	    var patt=/[\ud800-\udbff][\udc00-\udfff]/g; // 检测utf16字符正则
	    str = str.replace(patt, function(char){
            var H, L, code;
            if (char.length===2) {
                H = char.charCodeAt(0); // 取出高位
                L = char.charCodeAt(1); // 取出低位
                code = (H - 0xD800) * 0x400 + 0x10000 + L - 0xDC00; // 转换算法
                return "&#" + code + ";";
            } else {
                return char;
            }
        });
	    return str;
	}

	this.html_decode = function(str)   
	{   
		var s = "";   
		if (str.length == 0) return "";   
		//s = str.replace(/&gt;/g, "&");   
		s = str.replace(/&lt;/g, "<");   
		s = s.replace(/&gt;/g, ">");   
		s = s.replace(/&nbsp;/g, " ");   
		s = s.replace(/&#39;/g, "\'");   
		s = s.replace(/&quot;/g, "\"");   
		s = s.replace(/<br>/g, "\n");   
		return s;   
	}

	// 检查HTML style or script
	this.checkHtmlTags = function(str){
		var reg = /^((\<(script)|(style)[\ \S]{0,}\>)|(\<\/(script)|(style)[\ \S]{0,}\>)){1,}\S*(\<\/(script)|(style)\>){0,}$/g;
		if(reg.test(str)){
			return false;
		}else{
			return true;
		}
	}

})